clear
cd 'C:\Data\data_eyelink2'
cd 'C:\Data\data_eyelink2\from_LOR\PIF'
folders=dir('*.2*');
folders(~[folders.isdir])=[];
% folders.name
for fo_num=1:length(folders);
    cd(folders(fo_num).name);
    bfiles=dir('*B.mat');
    disp(['folder: ' folders(fo_num).name ''])
    dir('*B.mat');
    for fi_num=1:length(bfiles)
        switch 1
            case length(bfiles(fi_num).name)==10 & ...% if file name looks like: ABC01B.mat
                    strfind(bfiles(fi_num).name,'B.mat')==6 &...
                    isa(bfiles(fi_num).name(1:3),'char') &...
                    ~isempty(str2num(bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1)))

                load(bfiles(fi_num).name)
                year=num2str(B.clock(1));
                if B.clock(2)>9
                    month=num2str(B.clock(2));
                else
                    month=['0' num2str(B.clock(2))];
                end     
                if B.clock(3)>9
                    day=num2str(B.clock(3));
                else
                    day=['0' num2str(B.clock(3))];
                end            
                recorded_date=[year '-' month '-' day];
                clear year month day task
                if isempty(find(B.t_state(2,:)=='C')) %instantanious
                    task(1)='i';
                else % previous
                    task(1)='p';
                end
                task(2)='i'  ;              
                if length(unique(round((B.timea(1:end-1,4)-B.timea(1:end-1,3))/50)*50))>1 %random gap
                    task(3)='r';
                else% fixed gap
                    task(3)='f';
                end
                new_filename=[bfiles(fi_num).name(1:3) '.' task '.' recorded_date '_' bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1) 'B.mat'];
                [status, result]=dos(['rename ' bfiles(fi_num).name ' ' new_filename]);
                if status
                    disp(result)
                end
                
            case length(bfiles(fi_num).name)==11 & ...% if file name looks like: ABC100B.mat
                    strfind(bfiles(fi_num).name,'B.mat')==7 &...
                    isa(bfiles(fi_num).name(1:3),'char') &...
                    ~isempty(str2num(bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1))) 
                
                load(bfiles(fi_num).name)
                year=num2str(B.clock(1));
                if B.clock(2)>9
                    month=num2str(B.clock(2));
                else
                    month=['0' num2str(B.clock(2))];
                end     
                if B.clock(3)>9
                    day=num2str(B.clock(3));
                else
                    day=['0' num2str(B.clock(3))];
                end            
                recorded_date=[year '-' month '-' day];
                clear year month day task
                if isempty(find(B.t_state(2,:)=='C')) %instantanious
                    task(1)='i';
                else % previous
                    task(1)='p';
                end
                task(2)='i'  ;              
                if length(unique(round((B.timea(1:end-1,4)-B.timea(1:end-1,3))/50)*50))>1 %random gap
                    task(3)='r';
                else% fixed gap
                    task(3)='f';
                end
                new_filename=[bfiles(fi_num).name(1:3) '.' task '.' recorded_date '_' bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1) 'B.mat'];
                [status, result]=dos(['rename ' bfiles(fi_num).name ' ' new_filename]);
                if status
                    disp(result)
                end
                
            case length(bfiles(fi_num).name)==22 & ...% if file name looks like: ABC.2005_12_07_01B.mat
                    strfind(bfiles(fi_num).name,'B.mat')==18 &...
                    isa(bfiles(fi_num).name(1:3),'char') &...
                    ~isempty(str2num(bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1))) 
                load(bfiles(fi_num).name)
                year=num2str(B.clock(1));
                if B.clock(2)>9
                    month=num2str(B.clock(2));
                else
                    month=['0' num2str(B.clock(2))];
                end     
                if B.clock(3)>9
                    day=num2str(B.clock(3));
                else
                    day=['0' num2str(B.clock(3))];
                end            
                recorded_date=[year '-' month '-' day];
                clear year month day task
                if isempty(find(B.t_state(2,:)=='C')) %instantanious
                    task(1)='i';
                else % previous
                    task(1)='p';
                end
                task(2)='i'  ;              
                if length(unique(round((B.timea(1:end-1,4)-B.timea(1:end-1,3))/50)*50))>1 %random gap
                    task(3)='r';
                else% fixed gap
                    task(3)='f';
                end
                new_filename=[bfiles(fi_num).name(1:3) '.' task '.' recorded_date '_' bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1) 'B.mat'];
                disp(['rename ' bfiles(fi_num).name ' ' new_filename]);
                [status, result]=dos(['rename ' bfiles(fi_num).name ' ' new_filename]);
                if status
                    disp(result)
                end
                
            case length(bfiles(fi_num).name)==21 & ...% if file name looks like: AC.2005_12_07_01B.mat
                    strfind(bfiles(fi_num).name,'B.mat')==17 &...
                    isa(bfiles(fi_num).name(1:2),'char') &...
                    ~isempty(str2num(bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1))) 
                load(bfiles(fi_num).name)
                year=num2str(B.clock(1));
                if B.clock(2)>9
                    month=num2str(B.clock(2));
                else
                    month=['0' num2str(B.clock(2))];
                end     
                if B.clock(3)>9
                    day=num2str(B.clock(3));
                else
                    day=['0' num2str(B.clock(3))];
                end            
                recorded_date=[year '-' month '-' day];
                clear year month day task
                if isempty(find(B.t_state(2,:)=='C')) %instantanious
                    task(1)='i';
                else % previous
                    task(1)='p';
                end
                task(2)='i'  ;              
                if length(unique(round((B.timea(1:end-1,4)-B.timea(1:end-1,3))/50)*50))>1 %random gap
                    task(3)='r';
                else% fixed gap
                    task(3)='f';
                end
                new_filename=[bfiles(fi_num).name(1:2) '.' task '.' recorded_date '_' bfiles(fi_num).name(strfind(bfiles(fi_num).name,'B.mat')-2:strfind(bfiles(fi_num).name,'B.mat')-1) 'B.mat'];
                disp(['rename ' bfiles(fi_num).name ' ' new_filename]);
                [status, result]=dos(['rename ' bfiles(fi_num).name ' ' new_filename]);
                if status
                    disp(result)
                end
                
            otherwise
                disp(['> ' bfiles(fi_num).name])
        end %switch file name style
    end %for fi_num=1:length(bfiles)        
    cd ..
end %for f_num=1:length(folders)
